part of common_lib_plugin;

class NumberInputFormatter extends TextInputFormatter {
  final int numberLength;
  final bool supportDecimal;

  NumberInputFormatter({this.numberLength = 0, this.supportDecimal = false});

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (numberLength != 0) {
      if (!supportDecimal && newValue.text.contains('.')) {
        return oldValue;
      }
      if (newValue.text.length > numberLength && newValue.text.length > oldValue.text.length) {
        return oldValue;
      }
    }
    if (newValue.text.length < oldValue.text.length) {
      return newValue;
    }
    if (!newValue.text.isNum) {
      return oldValue;
    }

    return newValue;
  }
}

class NumberLimitInputFormatter extends TextInputFormatter {
  final double? maxValue;
  final double? minValue;

  NumberLimitInputFormatter({
    this.maxValue,
    this.minValue,
  });

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (!newValue.text.isNum && newValue.text.isNotEmpty) {
      return oldValue;
    }
    double num = double.tryParse(newValue.text) ?? 0;
    if (minValue != null && num < minValue!) return newValue.copyWith(text: minValue.toString(), selection: oldValue.selection);
    if (maxValue != null && num > maxValue!) return newValue.copyWith(text: maxValue.toString(), selection: oldValue.selection);
    return newValue;
  }
}

class DecimalInputFormatter extends TextInputFormatter {
  //小数长度，-1表示不可输入小数，0表示不限制
  final int decimalLength;
  final int? integerLength;

  DecimalInputFormatter({
    this.decimalLength = 0,
    this.integerLength,
  });

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (decimalLength == -1) {
      if (newValue.text.contains('.')) {
        return oldValue;
      }
      if (integerLength != null && newValue.text.length > integerLength!) {
        return oldValue;
      }
      return newValue;
    }
    if (decimalLength != 0) {
      final list = newValue.text.split('.');

      if (list.length > 1) {
        ///校验小数部分位数
        if (list[1].length > decimalLength) {
          return oldValue;
        }

        ///校验整数部分是否为空
        if (list[0].isEmpty) {
          return oldValue;
        }
      }
    }
    if (integerLength != null) {
      if (newValue.text.split('.')[0].length > integerLength!) {
        return oldValue;
      }
    }
    if (newValue.text.length < oldValue.text.length) {
      return newValue;
    }
    if (!newValue.text.isNum) {
      return oldValue;
    }

    return newValue;
  }
}

class LengthInputFormatter extends TextInputFormatter {
  final int length;
  final bool isZn2;

  LengthInputFormatter({this.length = 0, this.isZn2 = false});

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (length != 0) {
      var l = isZn2 ? newValue.text.commonCharLength() : newValue.text.length;
      if (l > length && newValue.text.length > oldValue.text.length) {
        return oldValue;
      }
    }
    if (newValue.text.length < oldValue.text.length) {
      return newValue;
    }

    return newValue;
  }
}

/// 自定义兼容中文拼音输入法正则校验输入框
class PatternTextInputFormatter extends TextInputFormatter {
  final Pattern filterPattern;

  PatternTextInputFormatter({required this.filterPattern});

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.isComposingRangeValid) return newValue;
    return FilteringTextInputFormatter.allow(filterPattern).formatEditUpdate(oldValue, newValue);
  }
}
